package com.artron.mediaartron.base;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-13.
 */

public abstract class BaseFragmentPagerAdapter extends FragmentPagerAdapter {

    private FragmentManager mFragmentManager;
    private ViewGroup mContainer;

    public BaseFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
        this.mFragmentManager = fm;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        this.mContainer = container;
        return super.instantiateItem(container, position);
    }

    public Fragment getFragment(int position) {
        if (mContainer != null) {
            String name = makeFragmentName(mContainer.getId(), getItemId(position));
            return mFragmentManager.findFragmentByTag(name);
        } else {
            return null;
        }

    }

    private static String makeFragmentName(int viewId, long id) {
        return "android:switcher:" + viewId + ":" + id;
    }
}
